﻿using System.Web;
using Domain.DbContext;

namespace Domain
{
    public class DbContextUtil
    {
        public static ModelContainer GetCurrentDbContext()
        {
            //HttpContext:一次请求，HttpContext已经保证只有一个实例
            //也可以使用一次进程一个实例
            ModelContainer dbContext = HttpContext.Current.Items["currentDbContext"] as ModelContainer;
            if (dbContext == null)//当前HttpContext不存在DbContext对象
            {
                dbContext = new ModelContainer();
                HttpContext.Current.Items.Add("currentDbContext", dbContext);
            }
            return dbContext;
        }
    }
}
